<?php
declare(strict_types=1);
namespace Service\GoodsService;
use Service\BaseService;
use App\Model\BbcModel\GoodsBatch;

/**
 * 商品批次服务中心
 * @author chenshubo
 */
class GoodsBatchService extends BaseService
{
    /**
     * 获取商品规格价格
     * @param array $goodsIds
     */
    public function getGoodsBatchList(int $mn_sid, array $goodsIds)
    {
        return GoodsBatch::instance()
        ->select([
            'id',
            'goods_id',
            'batch_no',
            'num',
            'expired_date'
        ])
        ->where('mn_sid',$mn_sid)
        ->whereIn('goods_id',$goodsIds)
        ->where('num','>',0)
        ->where('type','=',1)
        ->get()
        ->toArray();
    }
    
    /**
     * 获取商品规格价格
     * @param array $goodsIds
     */
    public function getGoodsBatchListByIDs(int $mn_sid, array $goodsIds, array $batchIds)
    {
        return GoodsBatch::instance()
        ->select([
            'id',
            'goods_id',
            'batch_no',
            'num',
            'expired_date',
            'store_isCombineOrders',
        ])
        ->where('mn_sid',$mn_sid)
        ->whereIn('goods_id',$goodsIds)
        ->whereIn('id',$batchIds)
        ->where('num','>',0)
        ->where('type','=',1)
        ->get()
        ->toArray();
    }

    /**
     * 获取分组功能
     * 商品
     *         |_规格
     * @param array $data
     */
    public function getMapGoodsBatchList( array $data )
    {
        if( empty($data) ) {
            return [];
        }
    
        $temp = [];
        foreach ($data as $item)
        {
            $temp[$item['goods_id']][$item['id']] = $item;
            unset($item);
        }
        return $temp;
    }
    
    /**
     * 获取分组功能
     * 商品
     *         |_规格
     * @param array $data
     */
    public function getMapList( array $data )
    {
        if( empty($data) ) {
            return [];
        }
    
        $temp = [];
        foreach ($data as $item)
        {
            $temp[$item['goods_id']][] = $item;
            unset($item);
        }
    
        return $temp;
    }

    /**
     * 获取商品规格价格.无店铺ID
     * @param int $cid
     * @param array $goodsIds
     */
    public function getChannelGoodsBatchList(int $cid,array $goodsIds)
    {
        return Goodsbatch::instance()
            ->select([
                'id',
                'goods_id',
                'batch_no',
                'num',
                'expired_date'
            ])
            ->whereIn('goods_id',$goodsIds)
            ->where('df',0)
            ->where('type',0)
            ->get()
            ->toArray();
    }
}